package logic

import (
	"context"

	"mail/service/property/rpc/internal/svc"
	"mail/service/property/rpc/property"
	"mail/service/property/model"
	"github.com/zeromicro/go-zero/core/logx"
	"google.golang.org/grpc/status"
)

type CreateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLogic {
	return &CreateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateLogic) Create(in *property.CreateRequest) (*property.CreateResponse, error) {
	// todo: add your logic here and delete this line
	newProperty := model.Property{
		Name:in.Name,
		Desc:in.Desc,
		Stock:in.Stock,
		Amount:in.Amount,
		Status:in.Status,
	}
	res,err:=l.svcCtx.PropertyModel.Insert(l.ctx,&newProperty)
	if err!=nil{
		return nil,status.Error(500,err.Error())
	}
	newPropertyId,err := res.LastInsertId()
	if err!=nil {
		return nil,status.Error(500,err.Error())
	}
	return &property.CreateResponse{
		Id:newPropertyId,
	},nil
}
